﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using AutoShutdownDotNet.Models;

namespace AutoShutdownDotNet
{
    public partial class SettingForm : Form
    {
        private bool passwordIsCorrect = false;

        #region Properties

        public bool StartWithWindows
        {
            get { return chkStartup.Checked; }
            set { chkStartup.Checked = value; }
        }

        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }

        public string CurrentPassword
        {
            get { return txtCurrentPassword.Text; }
            set { txtCurrentPassword.Text = value; }
        }

        public bool PasswordProtection
        { get { return chkEnablePassword.Checked; }
            set { chkEnablePassword.Checked = value; }
        }

        #endregion

        public void MakeStartup(bool enable)
        {
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if(enable)
                registryKey.SetValue("AutoShutdown.NET", Application.ExecutablePath.ToString());
            else
                registryKey.DeleteValue("AutoShutdown.NET",false);
        }


        public SettingForm()
        {
            InitializeComponent();
            Setting setting = Setting.GetDefault();
            if(setting != null)
            {
                PasswordProtection = (setting.Password != string.Empty);
                StartWithWindows = setting.StartWithWindows;
                txtCurrentPassword.Enabled = PasswordProtection;
                txtCurrentPassword.ReadOnly = !PasswordProtection;
            }
        }

        public bool ValidateForm()
        {
            Setting setting = Setting.GetDefault();
            if ((txtCurrentPassword.Text != string.Empty && !setting.PasswordProtected) || (setting.PasswordProtected))
            {
                if (setting.Password != MD5Coder.Encode(txtCurrentPassword.Text))
                {
                    MessageBox.Show("The Password you entered is not valid.", "Password", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    txtCurrentPassword.SelectAll();
                    txtCurrentPassword.Focus();
                    return false;
                }
                else
                    passwordIsCorrect = true;
            }
            if(PasswordProtection)
            {
                if((txtPassword.Text == string.Empty || txtRetypePassword.Text == string.Empty) && !passwordIsCorrect)
                {
                    MessageBox.Show("You need to create a password first or uncheck 'Enable Password Protection' option.",
                        "Password", MessageBoxButtons.OK,MessageBoxIcon.Information);
                    txtPassword.Focus();
                    return false;
                }
                if(txtPassword.Text != txtRetypePassword.Text)
                {
                    MessageBox.Show("Paswords did not match.", "Password", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    txtRetypePassword.Focus();
                    return false;
                }
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(ValidateForm())
            {
                Setting setting = Setting.GetDefault();
                if(setting == null)
                {
                    setting = new Setting();
                }
                if (PasswordProtection && Password != string.Empty)
                    setting.Password = MD5Coder.Encode(txtPassword.Text);
                if (!PasswordProtection)
                    setting.Password = string.Empty;
                if (setting.StartWithWindows != StartWithWindows)
                {
                    setting.StartWithWindows = StartWithWindows;
                    MakeStartup(StartWithWindows);
                }
                Setting.Update(setting);
                Close();
            }
        }

        private void chkEnablePassword_CheckedChanged(object sender, EventArgs e)
        {
            txtPassword.Enabled = txtRetypePassword.Enabled = chkEnablePassword.Checked;
            txtPassword.ReadOnly = txtRetypePassword.ReadOnly = !chkEnablePassword.Checked;
        }
    }
}
